﻿using AutoMapper;
using MediatR;
using ProduceBasicData.Api.Application.Command.ProductionManage;
using ProduceBasicData.Domain.ProductionManage;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;
using Yitter.IdGenerator;

namespace ProduceBasicData.Api.Application.Handler.ProductionManage
{
    /// <summary>
    /// 修改工单处理
    /// </summary>
    public class UpdateWorkCommandHandler : IRequestHandler<UpdateWorkCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<Work_order> workRespository;
        private readonly IMapper mapper;

        public UpdateWorkCommandHandler(IBaseRespository<Work_order> workRespository, IMapper mapper)
        {
            this.workRespository = workRespository;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(UpdateWorkCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>()
            {
                
                Code = ApiEnum.Success,
                Msg = "修改生产工单成功"
            };
            var time = DateTime.Now;
            var info = mapper.Map<Work_order>(request);
            info.UpdateName = request.UpdateName;
            info.UpdateTime = time;
            result.Data = await workRespository.Update(info);

            return await Task.FromResult(result);
        }
    }
}
